/*
 * @Author       : NaN 353362604@qq.com
 * @Date         : 2023-06-29 14:12:18
 * @LastEditTime : 2023-06-29 14:22:10
 * @LastEditors  : NaN 353362604@qq.com
 * @FilePath     : \AdminStration\src\Common\Router\Router.jsx
 * @Description  :
 */

import { useEffect } from 'react'
import { useBoolean } from 'ahooks'
import { lazy, Suspense } from 'react'
import { BrowserRouter, useRoutes } from 'react-router-dom'
import { ConfigProvider } from 'antd'
import zhCN from 'antd/locale/zh_CN'
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
import Loading from '../Currency/Loading'
// import { project } from '../Data'
import { VersionTips } from './MenuNav/DiyList/VersionTips'
import { FrontRouter } from './FrontRouter'

const { hostname } = window.location
// const { client_id } = project[hostname]

// 通用页面
const Login = lazy(() => import('../Login'))
const NO404 = lazy(() => import('../Currency/404'))
const Admin = lazy(() => import('../../Pages/Admin/Admin'))
const MapPage = lazy(() => import('../../Pages/Admin/Other/MapPage'))
const CodePage = lazy(() => import('../../Pages/Admin/Other/CodePage'))

// 路由
const RouterArr = [
    // 定义页面路径
    { path: '/', element: <Login /> },
    { path: '*', element: <NO404 /> },
    { path: 'Admin', element: <Admin /> },
    { path: 'MapPage', element: <MapPage /> },
    { path: 'VersionTips', element: <VersionTips /> },
    { path: 'Qrcode/:uuid', element: <CodePage /> },
    hostname !== 'localhost' && FrontRouter,
]

const Router = () => {
    dayjs.locale('zh-cn')
    const App = () => useRoutes(RouterArr)
    const [EdiTionState, { setFalse }] = useBoolean(true)
    useEffect(() => {
        const MinimumVersion = { Chrome: 93, Safari: 530, Firefox: 92, Opera: 80 }
        const { userAgent } = window.navigator
        const webList = userAgent
            .split(' ')
            .filter(v => v.indexOf('/') !== -1 && v.indexOf('Mozilla') === -1 && v.indexOf('AppleWebKit') === -1)
        const editionData = webList.map(key => ({ [key.split('/')[0]]: parseInt(key.split('/')[1]) }))
        editionData.forEach(key => {
            const [type] = Object.keys(key)
            const [edition] = Object.values(key)
            if (edition <= MinimumVersion[type]) setFalse()
        })
    }, [setFalse])
    return (
        <ConfigProvider locale={zhCN}>
            <BrowserRouter>
                <Suspense fallback={<Loading />}>{EdiTionState ? <App /> : <VersionTips />}</Suspense>
            </BrowserRouter>
        </ConfigProvider>
    )
}

export default Router
